package com.hiinfo.mvcdemo.weather;

import android.util.Log;

import com.hiinfo.mvcdemo.base.bean.BaseObjectBean;
import com.hiinfo.mvcdemo.net.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * @author : lifei
 * 创建时间： 19-6-28
 * 更改时间： 19-6-28
 * Email : allen.lee723@qq.com
 * @version :
 * Description：
 */
public class WeatherModelImpl implements WeatherModel {

    private static final String TAG = WeatherModelImpl.class.getSimpleName();

    private Call<BaseObjectBean<WeatherInfo>> mCall;

    public WeatherModelImpl() {

    }

    @Override
    public void queryWeather(OnWeatherListener callback) {
        mCall = RetrofitClient.get()
            .create(WeatherRequest.class)
            .queryWeather();
        mCall.enqueue(new Callback<BaseObjectBean<WeatherInfo>>() {

            @Override
            public void onResponse(retrofit2.Call<BaseObjectBean<WeatherInfo>> call, Response<BaseObjectBean<WeatherInfo>> response) {

                BaseObjectBean<WeatherInfo> weatherData = response.body();
                if (weatherData == null) {
                    Log.i(TAG, "onResponse: call is null");
                    return;
                }
                callback.onSuccess(weatherData.getWeatherinfo());
            }

            @Override
            public void onFailure(retrofit2.Call<BaseObjectBean<WeatherInfo>> call, Throwable t) {
                if (call.isCanceled()) {
                    Log.i(TAG, "call is canceled.");
                } else {
                    callback.onError(t);
                }
            }
        });
    }

    @Override
    public void cancelRequest() {
        if (mCall != null) {
            mCall.cancel();
        }
    }
}